﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using SouthApps.MobileHealth.SDK.DAL.Repositories;
using SouthApps.MobileHealth.SDK.Common.Exceptions;
using SouthApps.MobileHealth.SDK.DAL.Model;
using SouthApps.Framework.LoggerService;

namespace SouthApps.MobileHealth.SDK.Services
{
    public class SecurityService
    {
        ISecurityRepository securityRepository;

        public SecurityService()
        {
            this.securityRepository = new SecurityRepository();
        }

        public User Login(string username, string password, int financiadorId)
        {
            User user = this.securityRepository.GetUser(username, password, financiadorId);

            if (user != null)
            {
                this.securityRepository.UpdateUserLoginData(user.userId, DateTime.Now, true);
            }
            
            return user;
        }

        public List<Permission> GetUserPermissions(int userId)
        {
            List<Permission> userPermissions = this.securityRepository.GetUserPermissions(userId);

            return userPermissions;
        }

        public void Logout(int userId)
        {
            this.securityRepository.UpdateUserLoginData(userId, null, false);
        }

        public bool ValidateUserCredentials(string username, string password, int financiadorId)
        {
            if (this.securityRepository.GetUser(username, password, financiadorId) == null)
                return false;
            else
                return true;
        }
    }
}
